/**
 * 
 */
package monitor;

import java.util.ArrayList;


/**
 * @author Robbie
 *
 */
public class Application {
	
	private ArrayList<Thread> apps;
	
	public Application(){
		//do nothing
	}
	
	public Application(Presentation frame, RemoteHost host, int localPort){
		apps = new ArrayList<Thread>();
		PacketSender ps = new PacketSender(host,3000);
		PacketAnalyzer pa = new PacketAnalyzer(localPort);
		HeartBeatMaker hbm = new HeartBeatMaker();
		HeartBeatAnalyzer hba = new HeartBeatAnalyzer();
		ps.addPropertyChangeListener(pa);
		ps.addPropertyChangeListener(frame);
		pa.addPropertyChangeListener(ps);
		pa.addPropertyChangeListener(hba);
		pa.addPropertyChangeListener(frame);
		hba.addPropertyChangeListener(frame);
		hbm.addPropertyChangeListener(ps);
		apps.add(pa);	//PacketAnalyzer is always first
		apps.add(ps);	//PacketSender is always second
		apps.add(hbm);
		apps.add(hba);
	}
	
	public Application(Presentation frame, int localPort){
		apps = new ArrayList<Thread>();
		Tracker tracker = new Tracker(localPort);
		tracker.addPropertyChangeListener(frame);
		apps.add(tracker);
	}
	
	public void start(){
		if(apps.size()>1){
			((PacketAnalyzer)apps.get(0)).start();
			((PacketSender)apps.get(1)).setup(((PacketAnalyzer)apps.get(0)).getListenerPort());
			for(int loop=1;loop<apps.size();loop++){
				apps.get(loop).start();
			}
		}else{
			((Tracker)apps.get(0)).start();
		}
		
	}

}
